package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.system.domain.Department;
import com.ruoyi.system.domain.MeetingRoomApplication;
import com.ruoyi.system.domain.SysTeacher;
import com.ruoyi.web.controller.vo.EmployeeCountVoData;
import net.sf.ehcache.search.aggregator.Count;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Employee;
import com.ruoyi.system.service.IEmployeeService;
import com.ruoyi.system.service.IDepartmentService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.web.controller.vo.EmployeeCountVoData;
import com.ruoyi.common.vo.EmployeeCountVo;


/**
 * 员工信息Controller
 *
 * @author ruoyi
 * @date 2025-07-01
 */
@Controller
@RequestMapping("/system/employee")
public class EmployeeController extends BaseController {
    private String prefix = "system/employee";

    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;

    @RequiresPermissions("system:employee:view")
    @GetMapping()
    public String employee() {
        return prefix + "/employee";
    }

    /**
     * 根据部门id查找各个部门的员工数量(饼状图)
     */
    @ResponseBody
    @GetMapping("/countEmployeesNumberByDepartmentId")
    public AjaxResult countEmployeesNumberByDepartmentId() {
            //1.将mapper.xml中的数据取出，数据类型为EmployeeCountVo,存放在firstVo中
            List<EmployeeCountVo> list = employeeService.countEmployeesNumberByDepartmentId();

            //重点：新建两个list，用来为EmployeeCountVoData中的两个list赋值
            List<Integer> list1 = new ArrayList<>();
            List<String> list2 = new ArrayList<>();

            //这个增强for是使用EmployeeCountVo来为两个新建数组赋值
            for (EmployeeCountVo employeeCountVo : list) {
                list1.add(employeeCountVo.getEmployeeNumber());
                list2.add(employeeCountVo.getDepartmentName());
            }

            //部门数据接收完以后，再处理成两条List,一条List放所有部门名称，另一条List放各个部门的员工数量
            EmployeeCountVoData employeeCountVoData = new EmployeeCountVoData();
            employeeCountVoData.setEmpNums(list1);
            employeeCountVoData.setDeptNames(list2);

            //使用AjaxResult进行数据封装
            return AjaxResult.success("获取部门人数数据成功", employeeCountVoData);
    }


    /**
     * 查询员工信息列表
     */
    @RequiresPermissions("system:employee:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Employee employee) {
        startPage();
        List<Employee> list = employeeService.selectEmployeeList(employee);

        //TODO 实现通过部门id查到部门名字
        for (Employee employee1 : list) {
            Long departmentId = employee1.getDepartmentId();
            //根据部门的编号查询部门
            Department department = departmentService.selectDepartmentByDepartmentId(departmentId);
            if (department != null) {
                String departmentName = department.getDepartmentName();
                employee1.setDepartmentName(departmentName);
            }
        }

        return getDataTable(list);
    }

    /**
     * 导出员工信息列表
     */
    @RequiresPermissions("system:employee:export")
    @Log(title = "员工信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Employee employee) {
        List<Employee> list = employeeService.selectEmployeeList(employee);
        ExcelUtil<Employee> util = new ExcelUtil<Employee>(Employee.class);
        return util.exportExcel(list, "员工信息数据");
    }

    /**
     * 新增员工信息
     */
    @RequiresPermissions("system:employee:add")
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存员工信息
     */
    @RequiresPermissions("system:employee:add")
    @Log(title = "员工信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Employee employee) {
        return toAjax(employeeService.insertEmployee(employee));
    }

    /**
     * 修改员工信息
     */
    @RequiresPermissions("system:employee:edit")
    @GetMapping("/edit/{employeeId}")
    public String edit(@PathVariable("employeeId") Long employeeId, ModelMap mmap) {
        Employee employee = employeeService.selectEmployeeByEmployeeId(employeeId);
        mmap.put("employee", employee);
        return prefix + "/edit";
    }

    /**
     * 修改保存员工信息
     */
    @RequiresPermissions("system:employee:edit")
    @Log(title = "员工信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Employee employee) {
        return toAjax(employeeService.updateEmployee(employee));
    }

    /**
     * 删除员工信息
     */
    @RequiresPermissions("system:employee:remove")
    @Log(title = "员工信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(employeeService.deleteEmployeeByEmployeeIds(ids));
    }
}
